/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package	dao;

import Hibernate.HibernateUtil;
import pojo.Log;
import pojo.Website;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class LogDAO extends AbstractDAO {

	@Override
	protected Class getPOJOClass() {
		// TODO Auto-generated method stub
		return Log.class;
	}
	
    public Log getTopicByID(int id){
        Log log = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            log = (Log)session.get(Log.class, id);
        }
        catch(HibernateException ex){
            
        }
        finally{
            session.close();
        }
        return log;
    }
    
    public static synchronized boolean insert(String noidung)
    {
    	boolean flag= false;
    	Log log= new Log();
    	log.setLog(noidung);
    	log.setLogId(new Integer(0));
    	Date d=new Date();
    	log.setTime(d);
    	LogDAO dao= new LogDAO();
    	List<Log> l= dao.getList();
    	if(l.size()!=0)
    	{
    		Log t=l.get(l.size()-1);
    		log.setLogId(t.getLogId()+1);
    	}
    	Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();
			session.save(log);
			session.flush();
			transaction.commit();
			flag= true;
		} catch (HibernateException ex) {
			// Log the exception
			transaction.rollback();
			System.err.println(ex);
		}
		finally {
			session.close();
		}
		return flag;
	
    }
    public boolean deleteAll()
    {
    	boolean flag= false;
    	Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		LogDAO dao= new LogDAO();
		List<Log> l=dao.getList();
		try {
			transaction = session.beginTransaction();
			for(Log log : l)
			{
				session.delete(log);
			}
			session.flush();
			transaction.commit();
			flag= true;
		} catch (HibernateException ex) {
			// Log the exception
			transaction.rollback();
			System.err.println(ex);
		}
		finally {
			session.close();
		}
		return flag;
	
    }
    public boolean delete(Log log)
    {
    	boolean flag= false;
    	Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.delete(log);
			session.flush();
			transaction.commit();
			flag= true;
		} catch (HibernateException ex) {
			// Log the exception
			transaction.rollback();
			System.err.println(ex);
		}
		finally {
			session.close();
		}
		return flag;
	
    }
}
